/**
 * Created by 52835 on 2017/7/19.
 */

var systemSetDataPageSize = 10;

function fixSystemStringsInfo(dataArr) {
    var enableSettingsArr = {
        goldToScore: true,
        startGold: true,
        createUnionGold: true,
        maxUnionMemberCount: true,
    };

    var res = [];
    for (var i = 0; i < dataArr.length; ++i) {
        if (!!enableSettingsArr[dataArr[i].key]) {
            res.push(dataArr[i]);
        }
    }
    return res;
}

function createList() {
    apiAdminGetRecord('configModel', 0, 100, {}, function (data) {
        $('#dataList').bootstrapTable('load', fixSystemStringsInfo(data.msg.recordArr));
    });
}

function deleteParameter(key) {

    apiAdminDeleteRecord('configModel', { 'key': key }, function () {
        $.modal({ body: '删除成功' });
        createList();
    });
}

function addParameter(data, cb) {

    apiAdminAddRecord('configModel', data, function () {
        cb();
        createList();
    });
}

function updateParameter(matchData, saveData, cb) {
    apiAdminUpdateRecord('configModel', matchData, saveData, function () {
        cb();
        createList();
    });
}



function detailEdit(info = null) {

    if (info) {

        $("#keyID").val(info.key);
        $("#valueID").val(info.value);
        $("#uuid").val(info._id);
        $("#descriptionID").val(info.describe);
    } else {
        $("#keyID").val("");
        $("#valueID").val("");
        $("#descriptionID").val("");
    }
    $('#addNew').modal('show');
}


//保存
function saveConfig() {

    let uuid = $("#uuid").val() || null;
    let data = {
        key: $("#keyID").val(),
        value: $("#valueID").val(),
        describe: $("#descriptionID").val()
    }
    if (uuid) {

        let matchData = { '_id': uuid };
        updateParameter(matchData, data, function (err, result) {
            $.modal({ body: '修改成功' });
        });

    } else {
        addParameter(data, function (err, result) {
            $.modal({ body: '新增成功' });
        });
    }
    $('#addNew').modal('hide');

}






$(document).ready(function () {

    $('#dataList').bootstrapTable({
        striped: true, // 是否显示行间隔色
        pageNumber: 1, // 初始化加载第一页
        pagination: false, // 是否分页
        sidePagination: 'client', // server:服务器端分页|client：前端分页
        pageSize: 100, // 单页记录数
        pageList: [10, 20, 30],
        theadClasses: "thead-light",//这里设置表头样式
        totalField: "totalCount",
        smartDisplay: false,
        dataField: "recordArr",
        showRefresh: false,// 刷新按钮
        onPostBody: function () {
            //重新启用弹出层,否则formatter格式化函数返回的html字符串上的tooltip提示不生效
            $('[data-bs-toggle="tooltip"]').each(function (i, el) {
                new bootstrap.Tooltip(el)
            })
        },
        onLoadSuccess: function () {
            //重新启用弹出层,否则formatter格式化函数返回的html字符串上的tooltip提示不生效
            $('[data-bs-toggle="tooltip"]').each(function (i, el) {
                new bootstrap.Tooltip(el)
            })
        },
        columns: [{
            title: 'id',
            field: '_id',
            visible: false
        },
        {
            title: '键值',
            width: 100,
            align: "center",
            valign: "middle",
            field: 'key',
        }, {
            field: 'value',
            width: 300,
            align: "center",
            valign: "middle",
            title: '参数值'
        }, {
            title: '描述',
            width: 200,
            align: "center",
            valign: "middle",
            field: 'describe',
        },
        {
            title: '操作',
            width: 200,
            align: "center",
            valign: "middle",
            formatter: function (value, row, index) {
                let htm = `<button type = "button" class="btn btn-light"  data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="修改设置" onClick = 'detailEdit(${JSON.stringify(row)})' ><i class="bi bi-pencil-square fs-5 text-success"></i></button > `;
                htm += `<button type="button" class="btn btn-light"  data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="删除配置" onClick="deleteParameter('${row.key}',true)"><i class="bi bi-trash3-fill fs-5 text-danger"></i></button>`;
                return htm;
            }
        }]
    });
    createList();
});
